package com.cloudcc.mobile.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;

import com.cloudcc.cloudframe.adapter.CommonAdapter;
import com.cloudcc.cloudframe.adapter.ViewHolder;
import com.cloudcc.mobile.R;
import com.cloudcc.mobile.entity.beau.BeauListEntity;
import com.cloudcc.mobile.weight.SwipeLayout;
import com.cloudcc.mobile.weight.SwipeLayout.OnSwipeSlideListener;

/**
 * @Description: 对象列表适配器
 * @author wangzhiqiang
 * @date 2015-11-18 上午11:05:26
 * 
 */
public class BeauListAdapter extends CommonAdapter<BeauListEntity> {

	private int[] mSlideItemArray = new int[] { -1, -2 };

	private onItemMenuClickListener mOnItemMenuClickListener;

	private OnClickItem mOnClickItem;

	public OnClickItem getmOnClickItem() {
		return mOnClickItem;
	}

	public void setmOnClickItem(OnClickItem mOnClickItem) {
		this.mOnClickItem = mOnClickItem;
	}

	public BeauListAdapter(Context mContext) {
		super(mContext);
	}

	public void resetMenu() {
		if (mSlideItemArray[0] != -1 || mSlideItemArray[1] != -2) {
			mSlideItemArray[0] = -1;
			mSlideItemArray[1] = -2;
			notifyDataSetChanged();
		}
	}

	@Override
	public void convert(ViewHolder holder, BeauListEntity t) {
		holder.setText(R.id.tv1, t.getName());
		holder.setText(R.id.tv2, t.getCreatedate());
		SwipeLayout swipeContainer = holder.getView(R.id.swipe);
		if (mSlideItemArray[0] == holder.getPostion()) {
			swipeContainer.openMenu();
		} else if (mSlideItemArray[1] == holder.getPostion()) {
			swipeContainer.closeMenu();
		} else {
			swipeContainer.cloaseMenuWithOutAnt();
		}

		final int ops = holder.getPostion();
		swipeContainer.setOnSwipeSlideListener(new OnSwipeSlideListener() {
			@Override
			public void onOpen(SwipeLayout swipe) {
				setCurrentSlideItem(ops);
			}

			@Override
			public void onClose(SwipeLayout swipe) {
			}

		});
		final BeauListEntity entity = t;
		holder.getView(R.id.beau_call).setVisibility(
				TextUtils.isEmpty(t.getDianhua()) ? View.GONE : View.VISIBLE);

		holder.getView(R.id.beau_call).setOnClickListener(
				new OnClickListener() {
					@Override
					public void onClick(View v) {
						if (mOnItemMenuClickListener != null) {
							mOnItemMenuClickListener.onClickPhone(ops, entity);
						}
					}
				});
		holder.getView(R.id.beau_chatter).setOnClickListener(
				new OnClickListener() {
					@Override
					public void onClick(View v) {
						if (mOnItemMenuClickListener != null) {
							mOnItemMenuClickListener
									.onClickChatter(ops, entity);
						}
					}
				});
		holder.getView(R.id.beau_edit).setOnClickListener(
				new OnClickListener() {
					@Override
					public void onClick(View v) {
						if (mOnItemMenuClickListener != null) {
							mOnItemMenuClickListener.onClickEdit(ops, entity);
						}
					}
				});
		ImageView followView = holder.getView(R.id.beau_follow);
		followView.setBackgroundResource(t.ismefollow?R.drawable.ico_beau_follow_no:R.drawable.ico_beau_follow);
		holder.getView(R.id.beau_follow).setOnClickListener(
				new OnClickListener() {
					@Override
					public void onClick(View v) {
						if (mOnItemMenuClickListener != null) {
							mOnItemMenuClickListener.onClickFollow(ops, entity);
						}
					}
				});
		final int pp = holder.getPostion();
		holder.getConvertView().setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (mOnClickItem != null) {
					mOnClickItem.onClickItem(pp, entity);
				}
			}
		});
	}

	private void setCurrentSlideItem(int pos) {
		System.arraycopy(mSlideItemArray, 0, mSlideItemArray, 1, 1);
		mSlideItemArray[0] = pos;
		notifyDataSetChanged();
	}

	public interface OnClickItem {
		void onClickItem(int pos, BeauListEntity t);
	}

	/**
	 * 设置点击侧滑菜单的时候的监听
	 * 
	 * @param listener
	 */
	public void setOnItemMenuClickListener(onItemMenuClickListener listener) {
		this.mOnItemMenuClickListener = listener;
	}

	public interface onItemMenuClickListener {

		/**
		 * 点击 电话的时候
		 * 
		 * @param pos
		 * @param t
		 */
		void onClickPhone(int pos, BeauListEntity t);

		/**
		 * 点击chatter
		 * 
		 * @param pos
		 * @param t
		 */
		void onClickChatter(int pos, BeauListEntity t);

		/**
		 * 点击edit
		 * 
		 * @param pos
		 * @param t
		 */
		void onClickEdit(int pos, BeauListEntity t);

		/**
		 * 点击跟随的
		 * 
		 * @param pos
		 * @param t
		 */
		void onClickFollow(int pos, BeauListEntity t);

	}

	@Override
	public int getLayoutId() {
		return R.layout.beau_list_item;
	}

}
